package org.ahuanlien.crm.web.controller;

import org.ahuanlien.crm.domain.Company;
import org.ahuanlien.crm.query.CompanyQueryObject;
import org.ahuanlien.crm.query.QueryObject;
import org.ahuanlien.crm.service.ICompanyService;
import org.ahuanlien.crm.util.JSONResult;
import org.ahuanlien.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("company")
public class CompanyController {

	@Autowired
	private ICompanyService companyService;

	@RequestMapping("selectAll")
	@ResponseBody
	public List<Company> selectAll(QueryObject qo) {
		return companyService.selectAll();
	}

	//打开页面和数据获取分开
	@RequestMapping("view")
	public String view() {
		return "company/view";
	}

	@RequestMapping("query")
	@ResponseBody
	public PageResult query(CompanyQueryObject qo) {
		PageResult result = companyService.query(qo);
		return result;
	}
	@RequestMapping("saveOrUpdate")
	@ResponseBody
	public JSONResult saveOrUpdate(Company company){
		try {
			if(company.getId()!= null){
				companyService.updateByPrimaryKey(company);
			}else{
				company.setState(true);
				companyService.insert(company);
			}
			return new JSONResult();
		} catch (Exception e) {
			e.printStackTrace();
			return new JSONResult().mark("保存失败");
		}
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public JSONResult delete(Long id) {
		try {
			companyService.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONResult.mark("操作失败");
		}
		return JSONResult.success("操作成功");
	}

	@RequestMapping("changeState")
	@ResponseBody
	public JSONResult changeState(Long id){
		if(id != null){
			companyService.changeState(id);
			return new JSONResult();
		}else {
			return new JSONResult().mark("操作失败！");
		}
	}
}
